package com.naodong.shenluntiku.mvp.view.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

import com.google.android.flexbox.FlexboxLayout;
import com.google.gson.Gson;
import com.naodong.shenluntiku.R;
import com.naodong.shenluntiku.mvp.model.bean.Assessment;
import com.naodong.shenluntiku.mvp.model.bean.EventBusTag;
import com.naodong.shenluntiku.mvp.model.bean.SubjectInfo;
import com.naodong.shenluntiku.mvp.view.widget.CircleTextView;
import com.yatatsu.autobundle.AutoBundleField;

import butterknife.BindView;
import butterknife.OnClick;
import me.shingohu.man.base.BaseMVPFragment;
import me.shingohu.man.di.component.AppComponent;
import me.shingohu.man.intergation.EventManager;
import me.shingohu.man.util.UiUtils;

/**
 * Created by hujie on 17/5/10.
 */
public class AssessmentAnswerSheetFragment extends BaseMVPFragment {

    @BindView(R.id.flexbox)
    FlexboxLayout flexboxLayout;
    @AutoBundleField(required = false)
    Assessment assessment;
    LayoutInflater layoutInflater;


    @BindView(R.id.unAnswerTipView)
    View unAnswerTipView;


    @BindView(R.id.submitBtn)
    Button submitBtn;


    String lastAssessmentString = null;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {

    }

    @Override
    protected boolean userAutoBindBundle() {
        return true;
    }

    public static AssessmentAnswerSheetFragment getInstance(Assessment assessment) {
        AssessmentAnswerSheetFragment frgment = new AssessmentAnswerSheetFragment();
        frgment.setAssessment(assessment);
        return frgment;
    }

    @Override
    protected void onViewCreated(Bundle savedInstanceState) {
        layoutInflater = LayoutInflater.from(mActivity);
        setAnswerSheetView();
    }

    public void setAssessment(Assessment assessment) {
        this.assessment = assessment;
        lastAssessmentString = new Gson().toJson(assessment);
    }


    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        if (!assessment.isFinish() && isAssessmentUpdate()) {
            setAnswerSheetView();
        }
    }

    /**
     * 测评数据是否发生变化
     * @return
     */
    private boolean isAssessmentUpdate() {
        boolean flag = true;
        String currrentString = new Gson().toJson(assessment);
        if (currrentString.equals(lastAssessmentString)) {
            flag = false;
        }
        lastAssessmentString = currrentString;
        return flag;
    }


    private void setAnswerSheetView() {
        flexboxLayout.removeAllViews();

        int width = (UiUtils.getScreenWidth() - UiUtils.dip2px(10) * 2) / 5;
        int height = UiUtils.dip2px(60);

        for (int i = 0; i < assessment.getAssessNum(); i++) {
            SubjectInfo subjectInfo = assessment.getSubjects().get(i);
            View view = layoutInflater.inflate(R.layout.answersheet_option, null);
            CircleTextView circleTextView = (CircleTextView) view.findViewById(R.id.opTV);
            circleTextView.setText((i + 1) + "");
            FlexboxLayout.LayoutParams lp = new FlexboxLayout.LayoutParams(width, height);
            view.setLayoutParams(lp);


            if (subjectInfo.getuChooseIds().size() > 0) {
                circleTextView.setBgColor(R.color.color62B6E8);
            } else {
                circleTextView.setBgColor(R.color.color929292);
            }
            int finalI = i;
            view.setOnClickListener(v -> EventManager.getInstance().postIntEvent(EventBusTag.TO_ASSIGN_SUBJECT, finalI));

            flexboxLayout.addView(view);
        }
    }


    @OnClick(R.id.submitBtn)
    public void submitClick() {
        EventManager.getInstance().postEvent(EventBusTag.SUBMIT_ASSESSMENT);
    }


    @Override
    protected int layoutId() {
        return R.layout.f_answer_sheet;
    }
}
